<template>
  <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
    <el-form-item label="账号：" prop="userName">
      <el-input v-model="ruleForm.userName" />
    </el-form-item>
    <el-form-item label="密码：" prop="passWord">
      <el-input v-model="ruleForm.passWord" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>

    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { loginApi } from '@/api/api'
import { ElMessage } from 'element-plus'
import router from '@/router'
const ruleFormRef = ref()
const ruleForm = reactive({
  userName: '',
  passWord: '',
})

const rules = reactive({
  userName: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 8, message: 'Length should be 3 to 8', trigger: 'blur' },
  ],
  passWord: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 8, message: 'Length should be 3 to 8', trigger: 'blur' },
  ],
})
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {

      loginApi(ruleForm.userName, ruleForm.passWord).then(res => {
        localStorage.getItem('token', res.data.token);
        if (res.data.code == 1) {
          ElMessage({
            message: '登陆成功.',
            type: 'success',
          })
          router.push('/login')
        }
        else {
          ElMessage.error('登录失败.')
        }
      })

    }
  })
}
</script>

<style lang="scss" scoped></style>